package com.aaa.service.impl;

import com.aaa.dao.DictDataDao;
import com.aaa.dao.DictTypeDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.DictData;
import com.aaa.entity.DictType;
import com.aaa.entity.User;
import com.aaa.service.DictDataService;
import com.aaa.vo.DictTypeVo;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class DictServiceImpl extends ServiceImpl<DictDataDao, DictData> implements DictDataService {
    @Autowired
    private DictDataDao dictDataDao;
    @Autowired
    private DictTypeDao dictTypeDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<List<DictData>> findByType(String type) {
        try{
            String s = redisTemplate.opsForValue().get(type);
            if(StringUtils.hasText(s)){
                List<DictData> dictData = JSON.parseArray(s, DictData.class);
                return new Result<>(2000,"查询成功",dictData);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        List<DictData> list = dictDataDao.selectList(wrapper);
        return new Result<>(2000,"查询成功",list);
    }


    @Override
    public Result<String> cacheAsync() {
        //查询所有数据字典类型
        QueryWrapper<DictType> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<DictType> dictTypes = dictTypeDao.selectList(wrapper);
        //查询所有数据字典的数据
        //sys_user_sex---->[男，女，未知]
        for (DictType dictType : dictTypes) {
            QueryWrapper<DictData> wrapper2=new QueryWrapper<>();
            wrapper2.eq("status",0);
            wrapper2.eq("dict_type",dictType.getDictType());
            List<DictData> dictDatas = dictDataDao.selectList(wrapper2);
            redisTemplate.opsForValue().set(dictType.getDictType(), JSON.toJSONString(dictDatas));
        }
        return new Result<String>(2000,"缓存同步成功");
    }

    @Override
    public Result<IPage<DictData>> findByConditions(Integer currentPage, Integer pageSize, DictTypeVo dictVo) {
        IPage<DictData> page=new Page<>(currentPage,pageSize);
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(dictVo.getDictName())){
            wrapper.like("dict_name",dictVo.getDictName());
        }
        if(StringUtils.hasText(dictVo.getDictType())){
            wrapper.eq("dict_type",dictVo.getDictType());
        }
        if(Objects.nonNull(dictVo.getStatus())){
            wrapper.eq("status",dictVo.getStatus());
        }
        if(Objects.nonNull(dictVo.getDateRange())&&dictVo.getDateRange().length==2){
            wrapper.between("create_time",dictVo.getDateRange()[0],dictVo.getDateRange()[1]);
        }

        IPage<DictData> page1 = dictDataDao.selectPage(page, wrapper);
        return new Result<IPage<DictData>>(2000,"查询用户成功",page1);
    }

    //模块接口
    @Override
    public List<DictData> findHByType(String type) {
        try {
            String s = redisTemplate.opsForValue().get(type);
            if (StringUtils.hasText(s)) {
                List<DictData> dictData = JSON.parseArray(s, DictData.class);
                return dictData;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq("dict_type", type);
        List<DictData> dictData = dictDataDao.selectList(qw);
        return dictData;
    }
}
